﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class CustomerPODetail : System.Web.UI.Page
    {
        Helper helper = new Helper(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["custPOId"] != null)
                    {
                        int custPOId = Convert.ToInt32(Request.QueryString["custPOId"]);
                        CustomerPOController customerPOController = new CustomerPOController();
                        CustomerPOEntity customerPOEntity = customerPOController.findCustomerPOById(custPOId);
                        if (customerPOEntity.Status != "new")
                        {
                            //EditButton.Enabled = false;
                            DeleteButton.Enabled = false;
                        }
                        CustomerPOItemController customerPOItemController = new CustomerPOItemController();
                        IList<CustomerPOItemEntity> customerPoItemEntities =
                            customerPOItemController.findCustomerPOItemsByCustPOId(custPOId);
                        CustomerPOitemGridView.DataSource = customerPoItemEntities;
                        CustomerPOitemGridView.DataBind();

                        //Bind the customer po info.
                        CustomerPONoLabel.Text = customerPOEntity.No;
                        CreateDateLabel.Text = customerPOEntity.Date.ToString("d MMM, yyyy");
                        CustomerLabel.Text = customerPOEntity.CustomerShortName;
                        POStatusLabel.Text = customerPOEntity.Status;
                        AddressLabel.Text = customerPOEntity.Address;
                        ZipCodeLabel.Text = customerPOEntity.ZipCode;
                        AttentionLabel.Text = customerPOEntity.Attention;
                        PhoneLabel.Text = customerPOEntity.Phone;
                        FaxLabel.Text = customerPOEntity.Fax;
                        POTypeLabel.Text = customerPOEntity.Type;
                        CreatedByLabel.Text = customerPOEntity.CreatedBy.ToString();
                        ApprovedByLabel.Text = customerPOEntity.ApprovedBy.ToString();
                        RefereceLabel.Text = customerPOEntity.Reference;
                        RemarkLabel.Text = customerPOEntity.Remark;
                        TotalQtyLabel.Text = customerPOEntity.TotalQty.ToString();
                        SummaryPOTypeLabel.Text = customerPOEntity.Type;
                        SummaryTotalOpenQtyLabel.Text = customerPOEntity.TotalOpenQty.ToString();
                    }
                    else
                    {
                        Response.Redirect("CheckSalesHistory.aspx",false);
                    }

                }
                DeleteButton.Attributes.Add("onclick", "javascript:return   confirm( 'Are you sure?')");
                this.PreRender += new EventHandler(UpdateFlashMessage);
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
                //Response.Redirect("Default.aspx");
                Session["FlashMessage"] = "Error: " + ex.Message;
                Session["FlashMessageType"] = "error";
            }
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }
        protected void EditButton_Click(object sender, EventArgs e)
        {
            int custPOId = Convert.ToInt32(Request.QueryString["custPOId"]);
            Response.Redirect("EditCustomerPO.aspx?customerPOId=" + custPOId);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerPOController customerPOController = new CustomerPOController();
                int custPOId = Convert.ToInt32(Request.QueryString["custPOId"]);
                customerPOController.DeleteCustomerPOByCustPOId(custPOId);
               
                Session["FlashMessage"] = "Success: You Have Deleted That Order And Its Order Items!";
                Session["FlashMessageType"] = "success";
                Response.Redirect("~/CustomerPOList.aspx", false);
            }
            catch(Exception ex)
            {
                Session["FlashMessage"] = "Error: You Cannot Delete This Order! Maybe Already Used By Others!";
                Session["FlashMessageType"] = "error";
            }
        }
    }
}